<template>
  <div class="article_right">
    <div class="nav">
      <ul>
        <li @click="classify(item,index)" :class="{'active':index==nav_index}" v-for="(item,index) in nav" :key="index">{{item}}</li>
      </ul>
      <serach @seach="seachArticle"></serach>
    </div>
    <div class="article_list">
      <ul v-if="isArticleData">
        <li @click="toArticle(item.articleid)" v-for="(item,index) in article" :key="index">
          <div class="text">
            <span class="title">{{item.title}}
            </span>
            <div class="content">
              <div class="article_content">
                <iframe :ref="item.articleid" 
              @load="setIframe(item.articleid)" 
              :src="item.detail" scrolling="no" seamless frameborder="0" height="200"></iframe>
              </div>
              <div class="imgList" :ref="item.articleid"></div>
            </div>
          </div>
          <div class="info">
            <div class="user_info">
              <img :src="item.picture" alt="">
              <span>{{item.name}}</span>
            </div>
            <div class="time">
              <svg class='icon' aria-hidden='true'>
                <use xlink:href='#icon-shijian'></use>
              </svg>
              <span>{{timer(item.time)}}</span>
            </div>
            <div class="visit">
              <svg class='icon' aria-hidden='true'>
                <use xlink:href='#icon-youlanrenshu'></use>
              </svg>
              <span>{{item.visit}}</span>
            </div>
            <div class="like">
              <svg class='icon' aria-hidden='true'>
                <use xlink:href='#icon-like'></use>
              </svg>
              <span>{{item.likeCount}}</span>
            </div>
            <div class="tag">
              <div class="tag_box" v-for="(t,i) in item.tag == '' ? [] : item.tag.split(',')" :key="i">
                <svg class='icon' aria-hidden='true'>
                  <use xlink:href='#icon-biaoqian'></use>
                </svg>
                <span>{{t}}</span>
              </div>
            </div>
          </div>
          <div class="istop" v-if="item.istop">
            <svg class='icon' aria-hidden='true'>
              <use xlink:href='#icon-zhiding'></use>
            </svg>
          </div>
        </li>
      </ul>
      <div class="not_data" v-else>
        <svg class='icon' aria-hidden='true'>
          <use xlink:href='#icon-baoqian'></use>
        </svg>
        <span>抱歉,找不到相关文章...</span>
      </div>
      <div class="paging" v-if="isArticleData">
        <button @click="paging--" :disabled="paging==1">《</button>
        <span>{{paging}}</span>
        <button @click="paging++" :disabled="pagingSum">》</button>
      </div>
    </div>
  </div>
</template>

<script>
import tool from '../../assets/js/tool';
import serach from '../info/serach.vue';
export default {
  components: { serach },
  name: 'Articleright',

  data() {
    return {
      nav: ['全部文章',...this.$store.state.articleClassify], // 文章菜单列表
      nav_index: 0, // 菜单切换索引
      paging: 1, // 分页索引
      count: 4, // 每页展示文章数量
      article: [] // 过滤后的文章数据集
    };
  },

  props: {
    // 所有文章数据集
    articleData: Array
  },

  computed: {
    // 返回时间格式
    timer(){
      return (time) => tool.GetTimer(time)
    },
    // 判断当前数据集是否为空
    isArticleData(){
      return this.article.length != 0
    },
    // 返回最大页数
    pagingSum(){
      return this.paging == Math.ceil(this.articleData.length/this.count)
    }
  },

  watch: {
    paging(val){
      this.article = this.articleData.slice((val-1)*this.count,val*this.count)
      document.body.scrollTop=document.documentElement.scrollTop=0
    },
    articleData: 'init'
  },

  mounted() {
    this.init()
  },

  methods: {
    // 初始化
    init(){
      this.article = this.articleData.slice(0,this.count)
      this.paging = 1
    },
    // 分类过滤
    classify(val,index){
      this.$emit('classifyArticle',val)
      this.nav_index = index
    },
    // 查询过滤
    seachArticle(val){
      this.$emit('seachArticle',val)
    },
    // 查阅文章
    toArticle(id){
      this.$router.push('/article/' + id)
    },
    // 加载预览文章
    setIframe(id){
      let iframeDoc = this.$refs[id][0].contentDocument || this.$refs[id][0].contentWindow.document
      if(iframeDoc.body.scrollHeight < 300)
        this.$refs[id][0].height = 50
      if(iframeDoc.images.length){
        this.$refs[id][1].innerHTML = ''
        this.$refs[id][1].append(iframeDoc.images[0])
        this.$refs[id][1].append(iframeDoc.images[1] || '')
      }else{
        this.$refs[id][1].innerHTML = ''
      }
    }
  },
};
</script>

<style scoped>
.article_right{
  background-color: white;
  margin-left: 40px;
}
.article_right .nav{
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgb(168, 168, 168);
  padding-right: 10px;
}
.article_right .nav ul{
  display: flex;
  flex: 1;
  font-size: 0.7rem;
  flex-wrap: wrap;
}
.article_right .nav li{
  width: 70px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border: 1px solid gray;
  margin: 10px;
  cursor: pointer;
  transition: all .3s;
}
.article_right .nav ul .active,.article_right .nav li:hover{
  background-color: green;
  color: white;
}
.article_right .article_list{
  width: 100%;
}
.article_list .istop{
  font-size: 4em;
  position: absolute;
  top: -12px;
  right: -5px;
}
.article_right .article_list li{
  cursor: pointer;
  border-bottom: 1px solid rgb(168, 168, 168);
  padding: 10px;
  position: relative;
}
.article_right .article_list .text{
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  overflow: hidden;
}
.article_right .article_list li:hover .title{
  transform: translate(30px);
}
.article_right .article_list .text .title{
  background-color: rgb(13, 252, 104);
  color: white;
  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 5px;
  transition: all .5s;
}
.article_right .article_list .text .title::after{
  display: block;
  content: '';
  position: absolute;
  top: 0;
  right: -30px;
  width: 0;
  height: 0;
  border-top: 15px solid transparent;
  border-left: 30px solid rgb(13, 252, 104);
  border-bottom: 15px solid transparent;
}
.article_right .article_list .text .title::before{
  display: block;
  content: '';
  position: absolute;
  top: 30px;
  left: 0;
  width: 0;
  height: 0;
  border-top: 8px solid rgb(13, 252, 104);
  border-left: 8px solid transparent;
}
.article_right .article_list .text .content{
  display: flex;
  justify-content: flex-start;
  width: 100%;
}
.content .article_content{
  min-width: 200px;
}
.article_list .text .content .imgList{
  display: flex;
  align-items: center;
  width: 300px;
}
.article_list .text .content .imgList img{
  width: 120px;
}
.article_right .article_list .text iframe{
  transform-origin: 0 0;
  transform: scale(0.7);
  cursor: pointer;
  padding: 10px 0px 10px 40px;
}
.article_list .info{
  display: flex;
  align-items: center;
  font-size: 0.7rem;
  color: gray;
  flex-wrap: wrap;
}
.article_list .info span{
  white-space: nowrap;
}
.article_list .info .user_info{
  display: flex;
  align-items: center;
  margin-left: 10px;
}
.article_list .info .user_info img{
  width: 30px;
  height: 30px;
}
.article_list .info .time,.info .visit,.info .tag .tag_box,.info .like{
  background-color: rgb(206, 206, 206);
  padding: 4px;
  display: flex;
  align-items: center;
  margin-left: 10px;
}
.article_list .info .tag{
  display: flex;
}
.article_list .info .icon{
  font-size: 1rem;
}
.article_list .not_data{
  font-size: 0.7rem;
  color: gray;
  text-align: center;
  padding: 30px;
}
.article_list .not_data .icon{
  font-size: 2rem;
}
.article_list .paging{
  display: flex;
  justify-content: flex-end;
  padding: 20px;
}
.article_list .paging button{
  display: inline-block;
  width: 25px;
  height: 30px;
  line-height: 30px;
  cursor: pointer;
  background-color: white;
  border: 1px solid gray;
  font-size: 0.5rem;
  text-align: center;
}
.article_list .paging span{
  text-align: center;
  font-size: 0.5rem;
  line-height: 30px;
  width: 20px;
  background-color: green;
  color: white;
}
@media screen and (max-width:800px) {
  .article .article_right{
    margin-top: 0px;
  }
  .article_right .nav{
    flex-direction: column;
  }
   .article_right .nav ul{
     justify-content: flex-start;
   }
   .article_list .tag{
     margin-top: 5px;
   }
   .seach{
     margin-bottom: 10px;
   }
}
</style>